function setOtherMonthsValues(srcObj) {
	var m1 = parseInt($(srcObj).val());
	var visitNO = parseInt($('input:radio[name="pmAnnualVisitsNo"]:checked')
			.val());
	// alert("m1 : " + m1);
	// alert("type : " + type);
	var nextVal = m1;
	$("select.weqa2eya_months").slice(1, visitNO).each(function(index) {
		nextVal += 12 / visitNO;
		if (nextVal > 12)
			nextVal = nextVal % 12;

		$(this).val(nextVal);
		// $(this).prop('selectedIndex',0);
	});
}

function _onSubmitDeviceSearchForm() {
	if ($("#deviceCode").val().trim() == ""
			&& $("#deviceSerial").val().trim() == "") {
		alert("من فضلك أدخل قيم لإتمام عملية البحث");
		return false;
	}
	return true;
}

function _onSubmitJobOrderSearchForm() {
	if ($("#jobOrderNo").val().trim() == ""
			&& $("#deviceCode").val().trim() == ""
			&& $("#deviceSerial").val().trim() == "") {
		alert("من فضلك أدخل قيم لإتمام عملية البحث");
		return false;
	}
	return true;
}

function getPPMVisit() {

	var selectedMonth = $("#visitMonth").val();
	var selectedYear = $("#visitYear").val();
	var deviceId = $("#deviceId").val();
	var dest = $("#dest").val();

	// if (selectedMonth > 0 && selectedYear > 0).prop("selectedIndex")

	if ($("#visitMonth").prop("selectedIndex") != 0
			&& $("#visitYear").prop("selectedIndex") != 0)
		$("#content_container").load("PPMVisitDisplay", {
			visitMonth : selectedMonth,
			visitYear : selectedYear,
			deviceId : deviceId,
			dest : dest
		}, function() {
			initPage()
		});

}

function initPage() {

	$("body").prop("class", "pure-skin-mine");
	$("form").prop("class", "pure-form");

	// init calendar elements
	$(".caldr").datepicker({
		dateFormat : 'dd/mm/yy',
		changeMonth : true,
		changeYear : true,
		yearRange : "-30:+5"
	});

	// init file upload elements
	initFileInputElements();

	// $("select:not(.auto_off)").each(function(){alert("init :: "+this.name + "
	// :: "+$(this).find("option[selected='selected']").size() )})
	// add default option to dropdown lists
	$("select:not(.auto_off)")
			.prepend("<option value=''>من فضلك اختر</option>").each(function() {
				if ($(this).find("option[selected='selected']").size() == 0)
					this.selectedIndex = 0
			});
	$("select.auto_add_all").prepend("<option value='0'>الكل</option>").each(
			function() {
				if ($(this).find("option[selected='selected']").size() == 0)
					this.selectedIndex = 0
			});

	// style buttons with pure framework
	$("input[type='button']").addClass("pure-button");
	$("input[type='submit']").addClass("pure-button pure-button-primary");

	// add margin between horizontal radio group
	$("input[type='radio']").css("margin-left", "2px");
	$("input[type='radio']").next().css("margin-right", "15px");

	// special css to handle the category radios properly
	$("input[type='radio'][name='category']").css("margin-right", "2px");
	$("input[type='radio'][name='category']").next().css("margin-left", "15px");

	// add margin between horizontal checkbox group
	$("input[type='checkbox']").css("margin-left", "2px");
	$("input[type='checkbox']").next().css("margin-right", "15px");

	appendCommonQueryStrToReportsMenuItems();

	$("form").attr('novalidate', 'novalidate');
	$("form")
			.validate(
					{

						// ignore: ":hidden:not(input:file)", // to allow hidden
						// fields to validated

						errorPlacement : function(error, element) { // to adjust
							// the radio
							// and
							// checkbox
							// group msg
							// placement
							if ($(element).is("input:file")) {
								if (element.closest("label").parent().find(
										"span[class='requiredField']").length > 0)
									error
											.insertAfter(element
													.closest("label")
													.parent()
													.find(
															"span[class='requiredField']"));
								else
									error.insertAfter(element.closest("label"))
							} else {
								// to adjust display of category radios error
								// message
								if (element.parent().prop("id") == "category_radio_container")
									element.parent().prepend(error);
								else
									error.appendTo(element.parent());
							}
						}
					// debug:false,
					// focusInvalid: false,
					// onfocusout: false
					// onkeyup: false
					// rules:{
					// plannedStartDate: {
					// required:true,
					// dpDate:true,
					// dpCompareDate: ['before', '#plannedEndDate']
					// },
					// plannedEndDate:{
					// required:true,
					// dpDate:true,
					// dpCompareDate: ['after', '#plannedStartDate']
					// }
					//			  
					// }
					});

	initRequiredFields();

	// $("form select[selectrequired]").each((function(){$(this).rules("add",
	// {selectrequired: true})}))
	// markAsRequired("form *[required]");
}

function initNotification(cPath) {
	// init the job order notifications socket
	/*
	 * var eventSource = new EventSource(cPath +
	 * "/JobOrderNotificationsEmitter"); eventSource.onmessage = function(event) {
	 * $("#job_order_notification_ph").html(event.data); //alert(event.data);
	 */
	// }
}

function appendCommonQueryStrToReportsMenuItems() {

	$("li.reports_menu_items > ul > li > a").each(
			function() {

				// initially equal to the original href
				var newHref = $(this).prop("href");

				if (newHref.indexOf("reqRepTitle=") == -1) { // if the common
					// query string
					// param does
					// not exist
					newHref += ((newHref.indexOf("?") == -1) ? "?" : "&")
							+ "reqRepTitle="
							+ encodeURIComponent($(this).html());
					$(this).prop("href", newHref);
				}
			});
}

// init the notifications websocket
var wsConnection;

function connect() {
	wsConnection = new WebSocket("ws://127.0.0.1:8080/MMP/JobOrdersNotifier");
	
	wsConnection.onopen = function(){
		   console.log('WebSocket connection has been opened');
	}

	wsConnection.onclose = function(){
		   console.log('WebSocket connection has been closed');
	}

	wsConnection.onerror = function(error){
		   console.log('WebSocket connection has error detected: ' + error);
	}

	wsConnection.onmessage = function(event) {
		var data = event.data;
		// var container = document.getElementById("container");
		// container.innerHTML=data;
		// alert(event.data);
		// if(data.trim().length == 0)
		// $("#notifications_Container").html("").parent("tr").hide();
		// else
		$("#notifications_Container").html(event.data);//.parent("tr").show();
	}
}

$(function() {
	connect()
});